package cn.zhaozhiqi.template;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;

import java.util.Map;
public class GeTuiTemplate {
    private ApiHelper apiHelper;

    public GeTuiTemplate(ApiHelper apiHelper) {
        this.apiHelper = apiHelper;
    }

    /**
     * 绑定别名
     * @param cid
     * @param alias
     */
    public void bindAlias(String cid,String alias){
        UserApi userApi = apiHelper.creatApi(UserApi.class);
        CidAliasListDTO dto = new CidAliasListDTO();
        CidAliasListDTO.CidAlias cidAlias = new CidAliasListDTO.CidAlias();
        cidAlias.setAlias(alias);
        cidAlias.setCid(cid);
        dto.add(cidAlias);
        userApi.bindAlias(dto);
    }

    /**
     * 发送个推消息
     * @param message
     * @param alias
     */
    public void pushToSingleByAlias(String message,String alias) {
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(message);
        pushDTO.setPushMessage(pushMessage);

        /*设置接收人信息*/
        Audience audience = new Audience();
        audience.addAlias(alias);
        pushDTO.setAudience(audience);
        /*设置接收人信息结束*/
        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByAlias(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }

    }
}
